from Enums import Direction
from Enums import ServiceType
from Enums import OpType
from Enums import PayloadType
from Configs import STRING
from Configs import BOOLEAN
from Configs import INT32
from Configs import INT32_VEC
from Configs import INT64
from Configs import INT64_VEC
from Configs import FLOAT
from Configs import FLOAT_VEC
from Configs import BYTES

FILED_UINT8 = 0
FILED_UINT8_VEC = 1
FILED_INT8 = 2
FILED_INT8_VEC = 3
FILED_UINT16 = 4
FILED_INT16 = 5
FILED_INT16_VEC = 6
FILED_UINT32 = 7
FILED_INT32 = 8
FILED_INT32_VEC = 9
FILED_INT64 = 10
FILED_UINT64 = 11
FILED_INT64_VEC = 12
FILED_STRING = 13
FILED_FLOAT = 14
FILED_FLOAT_VEC = 15

class Element(object):
    #     name	module	direction	service_type	op_type	operation_id	payload_type	"payload
    # text_table/stuct name"	description	package	SWRS SignalName

    # def __init__(self, mName, mModule, mDirection, mServiceType, mOpType, mOpCode, mPayloadType):
    #     self.mName: str = mName
    #     self.mModule: str = mModule
    #     self.mDirection: Direction = mDirection
    #     self.mServiceType: ServiceType = mServiceType
    #     self.mOpType: OpType = mOpType
    #     self.mOpCode: int = mOpCode
    #     self.mPayloadType: PayloadType = mPayloadType

    def __init__(self):
        self.mSheetName = ""
        self.mName: str = ""
        self.mModule: str = ""
        self.mDirection: Direction = Direction.Default
        self.mServiceType: ServiceType = Direction.Default
        self.mOpType: OpType = OpType.Default
        self.mOpCode: int = 0
        self.mPayloadType: PayloadType = PayloadType.Default
        self.mPayloadStr: str = ""
        self.mDescription: str = ""
        self.mPackage: bool = False
        self.mSWRSName: str = ""
        self.mSignalValue:int = 0

    def setSheetName(self, sheetName: str):
        self.mSheetName = sheetName

    def setName(self, name: str):
        self.mName = name

    def setModule(self, module: str):
        self.setModule = module

    def setDirection(self, direction: str):
        if direction == "AP->CP" :
            self.mDirection = Direction.AP_2_CP
        elif direction == "CP->AP" :
            self.mDirection = Direction.CP_2_AP

    def setServiceType(self, serviceType: str):
        '''
        '''
        # self.setServiceType = ServiceType.Default
        # 暂无使用

    def setOpType(self, opType: str):
        '''
        '''
        # self.setOpType = OpType.Default
        # 暂无使用

    def setOpCode(self, opCode: str):
        if len(opCode) != 0:
            self.mOpCode = int(opCode.replace("0x", ""), 16)

    def setPayloadType(self, payloadType: str):
        if payloadType == "uint8":
            self.mPayloadType = PayloadType.uint8
        elif payloadType == "uint16":
            self.mPayloadType = PayloadType.uint16
        elif payloadType == "uint32":
            self.mPayloadType = PayloadType.uint32
        elif payloadType == "uint64":
            self.mPayloadType = PayloadType.uint64
        elif payloadType == "struct":
            self.mPayloadType = PayloadType.struct
        elif payloadType == "int16":
            self.mPayloadType = PayloadType.int16
        elif payloadType == "int32":
            self.mPayloadType = PayloadType.int32
        elif payloadType == "bytes":
            self.mPayloadType = PayloadType.bytes

    def getPayloadConfigStr(self):
        if self.mPayloadType == PayloadType.uint8 or \
                self.mPayloadType == PayloadType.uint16 or \
                self.mPayloadType == PayloadType.int16 or \
                self.mPayloadType == PayloadType.uint32 or \
                self.mPayloadType == PayloadType.int32:
            return "INT32"
        elif self.mPayloadType == PayloadType.uint64:
            return "INT64"
        elif self.mPayloadType == PayloadType.struct:
            return "BYTES"
        else:
            return "ERROR"

    def getPayloadConfigStr(self):
        if self.mPayloadType == PayloadType.uint8 or \
                self.mPayloadType == PayloadType.uint16 or \
                self.mPayloadType == PayloadType.int16 or \
                self.mPayloadType == PayloadType.uint32 or \
                self.mPayloadType == PayloadType.int32:
            return "INT32"
        elif self.mPayloadType == PayloadType.uint64:
            return "INT64"
        elif self.mPayloadType == PayloadType.struct:
            return "BYTES"
        else:
            return "ERROR"

    def getPayloadConfigValue(self):
        if self.mPayloadType == PayloadType.uint8 or \
                self.mPayloadType == PayloadType.uint16 or \
                self.mPayloadType == PayloadType.int16 or \
                self.mPayloadType == PayloadType.uint32 or \
                self.mPayloadType == PayloadType.int32:
            return INT32
        elif self.mPayloadType == PayloadType.uint64:
            return INT64
        elif self.mPayloadType == PayloadType.struct:
            return BYTES
        else:
            return BYTES

    def getPayloadConfigValueInt(self):
        if self.mPayloadType == PayloadType.uint8:
            return FILED_UINT8
        if self.mPayloadType == PayloadType.uint16:
            return FILED_UINT16
        if self.mPayloadType == PayloadType.int16:
            return FILED_INT16
        if self.mPayloadType == PayloadType.uint32:
            return FILED_UINT32
        if self.mPayloadType == PayloadType.uint64:
            return FILED_UINT64
        if self.mPayloadType == PayloadType.int32:
            return FILED_INT32
        return FILED_UINT8


    def setPayloadStr(self, payloadStr):
        self.mPayloadStr = payloadStr

    def setDescription(self, description: str):
        self.mDescription = description

    def setPackage(self, package: str):
        if package == "TRUE":
            self.mPackage = True
        elif package == "" or package == "FALSE":
            self.mPackage = False

    def setSWRSName(self, swrsName: str):
        self.mSWRSName = swrsName

    def print(self):
        print("======================="
              "\nmName:        [" + str(self.mName) +
              "]\nmModule:      [" + str(self.mModule) +
              "]\nmDirection:   [" + str(self.mDirection.name) +
              "]\nmServiceType: [" + str(self.mServiceType.name) +
              "]\nmOpType:      [" + str(self.mOpType.name) +
              "]\nmOpCode:      [" + str("%#x" % self.mOpCode) +
              "]\nmPayloadType: [" + str(self.mPayloadType.name) +
              "]\nmDescription: [" + str(self.mDescription) +
              "]\nmPackage:     [" + str(self.mPackage) +
              "]\nmSWRSName:    [" + str(self.mSWRSName) +
              "]")

    def __str__(self):
        return "mName:        [" + str(self.mName) + \
               "]\nmModule:      [" + str(self.mModule) + \
               "]\nmDirection:   [" + str(self.mDirection.name) + \
               "]\nmServiceType: [" + str(self.mServiceType.name) + \
               "]\nmOpType:      [" + str(self.mOpType.name) + \
               "]\nmOpCode:      [" + str("%#x" % self.mOpCode) + \
               "]\nmPayloadType: [" + str(self.mPayloadType.name) + \
               "]\nmDescription: [" + str(self.mDescription) + \
               "]\nmPackage:     [" + str(self.mPackage) + \
               "]\nmSWRSName:    [" + str(self.mSWRSName) + \
               "]"

    pass
